﻿using System;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using Microsoft.SharePoint.Administration;
using ServBus.ListForm2010.Controls;
using ServBus.ListForm2010.License;
using ServBus.ListForm2010.Resources;

namespace ServBus.ListForm2010.AdministrationPages
{
    public partial class License : System.Web.UI.Page
    {
        #region Controls
        protected BreadCrumbNav bcNav;
        protected HtmlTable tblInfo;
        protected HyperLink lnkServerInfo;
        protected HyperLink lnkImport;
        protected Label lblProductName;
        protected Label lblFeatureSet;
        protected Label lblLicenseType;
        protected Label lblCompany;
        protected Label lblIssuedTo;
        protected Label lblIssuedDate;
        protected Label lblExpirationDate;
        protected Label lblIssuedBy;
        protected Label lblKey;
        protected Label lblServers;
        protected Label lblActivationLimit;
        protected Label lblActivationCurrent;
        protected Label lblError;
        #endregion

        private static DllResource Resource = new DllResource("ServBus.ListForm2010.Common.Resources|AdminPages");

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                // Nav
                bcNav.AddLink(Resource.GetString("License_Nav"), null, null);

                //errorAlert.Text = "空?" + LicenseContainer.IsLicenseEmpty + "有效?" + LicenseContainer.IsLicenseValid;
                if (LicenseContainer.IsLicenseEmpty || !LicenseContainer.IsLicenseValid)
                {
                    if (LicenseContainer.IsLicenseEmpty)
                        lblError.Text = Resource.GetString("License_NoLicense");
                    else
                        lblError.Text = Resource.GetString("License_InvalidLicense");
                    lnkImport.Text = Resource.GetString("License_Import");
                    lblServers.Text = Resource.GetString("License_None");
                    lblActivationLimit.Text = Resource.GetString("License_None");
                    lblActivationCurrent.Text = Resource.GetString("License_None");
                    tblInfo.Visible = false;
                    return;
                }

                ServBus.ListForm2010.License.License license = LicenseContainer.ProductLicense;
                lnkImport.Text = Resource.GetString("License_Update");
                lblProductName.Text = license.ProductName;
                lblFeatureSet.Text = license.FeatureSet.ToString();
                if (license.LicenseType == LicenseType.Trail)
                    lblLicenseType.Text = "Trial";
                else
                    lblLicenseType.Text = license.LicenseType.ToString();
                lblCompany.Text = license.Company;
                lblIssuedTo.Text = license.IssuedTo;
                lblIssuedDate.Text = license.IssuedDate.ToString();
                lblExpirationDate.Text = license.ExpirationDate.ToString();
                lblIssuedBy.Text = license.IssuedBy;
                lblKey.Text = license.LicenseKey;

                //errorAlert.Text += ";日期?" + LicenseContainer.ProductLicense.IssuedDate.ToString();

                // Registerd Server
                int idx = 0;
                foreach (SPServer server in FarmInfo.GetFrontEndServers())
                {
                    if (license.IsValidServer(server.Id))
                    {
                        if (idx > 0) lblServers.Text += "<br/>";
                        lblServers.Text += server.Name;
                        idx++;
                    }
                }

                // Activation Info
                Activations farmActs = new Activations(SPFarm.Local);
                if (license.ActivationLimit == int.MaxValue)
                {
                    lblActivationLimit.Text = Resource.GetString("License_UnLimit");
                }
                else
                {
                    lblActivationLimit.Text = license.ActivationLimit.ToString();
                }
                lblActivationCurrent.Text = farmActs.GetEffectiveCount().ToString();
            }
            catch (Exception errorOccured)
            {
                //errorAlert.Text += errorOccured.ToString();
            }
        }
    }
}
